package vip.xiaonuo.sys.modular.user.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
import vip.xiaonuo.sys.modular.relation.entity.SysRelation;
import vip.xiaonuo.sys.modular.relation.enums.SysRelationCategoryEnum;
import vip.xiaonuo.sys.modular.relation.service.SysRelationService;
import vip.xiaonuo.sys.modular.role.entity.SysRole;
import vip.xiaonuo.sys.modular.role.service.SysRoleService;
import vip.xiaonuo.sys.modular.user.entity.SysUser;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 读取用户角色到角色表
 */
@Slf4j
@Component
public class BizUserRoleTaskRunner implements CommonTimerTaskRunner {

    @Resource
    private SysRelationService sysRelationService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    @Override
    public void action(String extJson) {
        run();
    }

    public void run() {
        HashMap<String, SysRole> roles = getRoles();
        int pageSize = 100;
        int currentPage = 1;
        while (true) {
            List<SysUser> updateUsers = new ArrayList<>();
            Page<SysUser> page = new Page<>(currentPage, pageSize);
            Page<SysUser> userPage = sysUserService.page(page);
            List<SysUser> records = userPage.getRecords();
            if (records.isEmpty()) {
                break; // 没有更多数据了
            }
            // 处理当前批次的数据，例如插入、更新、导出等操作
            for (SysUser user : records) {
                SysUser sysUser = processUser(user, roles);
                if (sysUser != null) {
                    updateUsers.add(sysUser);
                }
            }
            currentPage++;
            if (!updateUsers.isEmpty()) {
                sysUserService.updateBatchById(updateUsers);
            }
        }
    }

    private HashMap<String, SysRole> getRoles() {
        List<SysRole> list = sysRoleService.list();
        HashMap<String, SysRole> map = new HashMap<>();
        list.forEach(sysRole -> map.put(sysRole.getId(), sysRole));
        return map;
    }

    private SysUser processUser(SysUser user, HashMap<String, SysRole> roles) {
        List<SysRelation> relationListByObjectIdAndCategory = sysRelationService.getRelationListByObjectIdAndCategory(user.getId(), SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue());
        if (relationListByObjectIdAndCategory.isEmpty()) {
            return null;
        }
        String extJson = user.getExtJson();
        JSONObject extJsonObj = new JSONObject();
        if (extJson != null) {
            extJsonObj = JSON.parseObject(extJson);
        }
        extJsonObj.put("roleName", roles.get(relationListByObjectIdAndCategory.get(0).getTargetId()).getName());
        user.setExtJson(extJsonObj.toJSONString());
        return user;
    }


}
